<?php
/**
 * @version		$Id: search.php 10752 2008-08-23 01:53:31Z eddieajau $
 * @package		Joomla
 * @subpackage	Search
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Search Component Search Model
 *
 * @package		Joomla
 * @subpackage	Search
 * @since 1.5
 */
class SimModelSim extends JModel
{
    /**
     * Sezrch data array
     * @var array
     */
	var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_limit = 30;
	var $_limitstart = 0;
	
	
	public function __construct(){
		
		parent::__construct();
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$limitstart = JRequest::getInt('limitstart');
		
		$this->_limitstart = empty($limitstart) ? 0 : $limitstart;
		
		$this->setState('limit', $this->_limit);
		$this->setState('limitstart', $this->_limitstart);
	}
	
    function &getData()
    {
        
		if (empty($this->_data)){
		
            $minprice = JRequest::getVar("minprice");
            if(strlen($minprice)>3){
                $minprice = substr($minprice,0,strlen($minprice)-3);
            }
            $maxprice = JRequest::getVar("maxprice");
            if(strlen($maxprice)>3){
                $maxprice = substr($maxprice,0,strlen($maxprice)-3);
            }
            $searchword = JRequest::getVar("searchword");
            $query = "select S.*,P.provider_name,A.admin_name from #__sim S,#__sim_provider P,#__sim_admin A where S.provider_id=P.provider_id and S.admin_id=A.admin_id and S.del_flag=0 and S.price >".$minprice.' and S.price<'.$maxprice.' ';
            if(JRequest::getVar("provider")){
                $provider = JRequest::getVar("provider");
                $query=$query.' and S.provider_id='.$provider;
            }
            if(JRequest::getVar("category")){
                $category = JRequest::getVar("category");
                $query=$query.' and S.categori_id='.$category;
            }
			$query = $query.' order by S.price desc';
		
            $rows = $this->_getList( $query );
            if($searchword==""){
                
				$this->_data = $rows;
            }
            else{
                
				$length = count($rows);
                foreach ($rows as $row)
                {
                    $sim_numb = str_replace(" ","",$row->sim_numb);
					$sim_numb = str_replace(".","",$sim_numb);
					$searchword= str_replace(" ","",$searchword);
					$searchword= str_replace(".","",$searchword);
                    if(substr_count($sim_numb, $searchword)>0){
                        $this->_data[]=$row;
                    }
                }
            }
        }
		
        return $this->_data;
    }
	
	public function getResult(){
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$query = $this->_buildQuery();
		//echo $query; exit;
		if (empty($this->_data)){
		
			$this->_db->setQuery( $query, $this->_limitstart, $this->_limit );
			$this->_data = $this->_db->loadObjectList();
		}
		
		$data = $this->_data;
		
		return $data;
		
	}
	
	public function _buildQuery(){
		
		//echo "<pre>";print_r($_GET);exit;
		
		//echo "<hr />";
		
		$minprice = JRequest::getVar("minprice");
		
		if(strlen($minprice)>3){
			$minprice = substr($minprice,0,strlen($minprice)-3);
		}
		
		$maxprice = JRequest::getVar("maxprice");
		
		if(strlen($maxprice)>3){
			
			$maxprice = substr($maxprice,0,strlen($maxprice)-3);
		}
		
		//echo $minprice."<br/>".$maxprice."<hr/>";exit;
		$searchword = JRequest::getVar("searchword");
		
		$query = "SELECT S.*,P.provider_name,A.admin_name 
				FROM #__sim S,#__sim_provider P,#__sim_admin A 
				WHERE 
					  S.provider_id = P.provider_id and 
					  S.admin_id    = A.admin_id    and 
					  S.del_flag    = 0             and 
					  S.price > " .$minprice. '     and 
					  S.price < ' .$maxprice. '        ';
					  
		//echo $query;exit;
		
		if(JRequest::getVar("provider")){
		
			$provider = JRequest::getVar("provider");
			$query=$query.' and S.provider_id='.$provider;
			
		}
		
		$Textsim = JRequest::getVar("Textsim");
		
		$n       = JRequest::getVar("n");
		
		
	
		if($Textsim && (!empty($Textsim))){
		
			$query .= ' and S.sim_numb LIKE "%'.$Textsim.'%"';
			
		}
		
		if($n && (is_array($n))){
			
			for($i = 0; $i < count($n); $i++){
				
				$query .= ' and S.sim_numb NOT LIKE "%'.$n[$i].'%"';
			}
		}
		
		if(JRequest::getVar("category")){
		
			$category = JRequest::getVar("category");
			$query = $query.' and S.categori_id='.$category;
		}
		
		$query = $query.' order by S.price desc';
		return $query;
		
	}
	public function getTotal(){
		
		if (empty($this->_total)){
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	public function getPagination(){
	
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->_limitstart, $this->_limit );
		}
		return $this->_pagination;
	}
	
}
